#include <stdio.h>
#include <math.h>

/* 定义结构体存储不同类型热电偶的参数 */
typedef struct
{
    float d[11];
    float error_min;
    float error_max;
    int order;
} InverseCoifficientsConfig;
/* 定义返回值类型 */
typedef struct
{
    float t;              // 温度
    float error_range[2]; // 误差范围
} InverseResult;

/* 使用霍纳法则计算多项式
 * t_90 = d_0 + d_1*E + d_2*E^2 + ...
 *     + d_n*E^n
 */
float evaluate_polynomial(const float *d, int n, float E)
{
    float result = d[n]; // 初始化最高次项系数
    for (int i = n - 1; i >= 0; i--)
    {
        result = result * E + d[i]; // 嵌套乘法
    }
    return result;
}
/**
 * @brief 根据热电偶配置参数计算温度及其误差范围
 *
 * 该函数使用传入的热电偶配置参数，通过霍纳法则计算多项式的值得到温度，并设置误差范围。
 *
 * @param config 指向 InverseCoifficientsConfig 结构体的指针，包含热电偶的多项式系数、误差范围和多项式阶数。
 * @param type 热电偶的类型，k/K,b/B,e/E,j/J,n/N,t/T,r/R,s/S。
 * @param E 测量得到的电压值。
 * @return InverseResult 包含计算得到的温度值和误差范围的结构体。
 */
InverseResult calculateT(InverseCoifficientsConfig *config, char type, float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    float sum = 0.0;
    sum += evaluate_polynomial(config->d, config->order, E);
    res.t = sum;
    res.error_range[0] = config->error_min;
    res.error_range[1] = config->error_max;
    return res;
}

/* K 型热电偶 */
InverseResult inverse_calculate_k(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -5.891 && E < 0.00)
    {
        InverseCoifficientsConfig k_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.04,
            .order = 9};
        k_config.d[0] = 0.0000000E+00;
        k_config.d[1] = 2.5173462E+01;
        k_config.d[2] = -1.1662878E+00;
        k_config.d[3] = -1.0833638E+00;
        k_config.d[4] = -8.9773540E-01;
        k_config.d[5] = -3.7342377E-01;
        k_config.d[6] = -8.6632643E-02;
        k_config.d[7] = -1.0450598E-02;
        k_config.d[8] = -5.1920577E-04;
        k_config.d[9] = 0.0000000E+00;

        res = calculateT(&k_config, 'k', E);
    }
    if (E >= 0.00 && E < 20.644)
    {
        InverseCoifficientsConfig k_config = {
            .d = {0},
            .error_min = -0.05,
            .error_max = 0.04,
            .order = 9};
        k_config.d[0] = 0.000000E+00;
        k_config.d[1] = 2.508355E+01;
        k_config.d[2] = 7.860106E-02;
        k_config.d[3] = -2.503131E-01;
        k_config.d[4] = 8.315270E-02;
        k_config.d[5] = -1.228034E-02;
        k_config.d[6] = 9.804036E-04;
        k_config.d[7] = -4.413030E-05;
        k_config.d[8] = 1.057734E-06;
        k_config.d[9] = -1.052755E-08;
        res = calculateT(&k_config, 'k', E);
    }
    if (E >= 20.644 && E <= 54.886)
    {
        InverseCoifficientsConfig k_config = {
            .d = {0},
            .error_min = -0.05,
            .error_max = 0.06,
            .order = 9};
        k_config.d[0] = -1.318058E+02;
        k_config.d[1] = 4.830222E+01;
        k_config.d[2] = -1.646031E+00;
        k_config.d[3] = 5.464731E-02;
        k_config.d[4] = -9.650715E-04;
        k_config.d[5] = 8.802193E-06;
        k_config.d[6] = -3.110810E-08;
        k_config.d[7] = 0.000000E+00;
        k_config.d[8] = 0.000000E+00;
        k_config.d[9] = 0.000000E+00;
        res = calculateT(&k_config, 'k', E);
    }
    return res;
}

/* B 型热电偶 */
InverseResult inverse_calculate_b(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= 0.291 && E < 2.431)
    {
        InverseCoifficientsConfig b_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.03,
            .order = 8};
        b_config.d[0] = 9.8423321E+01;
        b_config.d[1] = 6.9971500E+02;
        b_config.d[2] = -8.4765304E+02;
        b_config.d[3] = 1.0052644E+03;
        b_config.d[4] = -8.3345952E+02;
        b_config.d[5] = 4.5508542E+02;
        b_config.d[6] = -1.5523037E+02;
        b_config.d[7] = 2.9886750E+01;
        b_config.d[8] = -2.4742860E+00;
        res = calculateT(&b_config, 'b', E);
    }
    if (E >= 2.431 && E <= 13.820)
    {
        InverseCoifficientsConfig b_config = {
            .d = {0},
            .error_min = -0.01,
            .error_max = 0.02,
            .order = 8};
        b_config.d[0] = 2.1315071E+02;
        b_config.d[1] = 2.8510504E+02;
        b_config.d[2] = -5.2742887E+01;
        b_config.d[3] = 9.9160804E+00;
        b_config.d[4] = -1.2965303E+00;
        b_config.d[5] = 1.1195870E-01;
        b_config.d[6] = -6.0625199E-03;
        b_config.d[7] = 1.8661696E-04;
        b_config.d[8] = -2.4878585E-06;
        res = calculateT(&b_config, 'b', E);
    }
    return res;
}

/* E 型热电偶 */
InverseResult inverse_calculate_e(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -8.828 && E < 0.00)
    {
        InverseCoifficientsConfig e_config = {
            .d = {0},
            .error_min = -0.01,
            .error_max = 0.03,
            .order = 9};
        e_config.d[0] = 0.0000000E+00;
        e_config.d[1] = 1.6977288E+01;
        e_config.d[2] = -4.3514970E-01;
        e_config.d[3] = -1.5859697E-01;
        e_config.d[4] = -9.2502871E-02;
        e_config.d[5] = -2.6084314E-02;
        e_config.d[6] = -4.1360199E-03;
        e_config.d[7] = -3.4034030E-04;
        e_config.d[8] = -1.1564890E-05;
        e_config.d[9] = 0.0000000E+00;
        res = calculateT(&e_config, 'e', E);
    }
    if (E >= 0.00 && E <= 76.373)
    {
        InverseCoifficientsConfig e_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.02,
            .order = 9};
        e_config.d[0] = 0.0000000E+00;
        e_config.d[1] = 1.7057035E+01;
        e_config.d[2] = -2.3301759E-01;
        e_config.d[3] = 6.5435585E-03;
        e_config.d[4] = -7.3562749E-05;
        e_config.d[5] = -1.7896001E-06;
        e_config.d[6] = 8.4036165E-08;
        e_config.d[7] = -1.3735879E-09;
        e_config.d[8] = 1.0629823E-11;
        e_config.d[9] = -3.2447087E-14;
        res = calculateT(&e_config, 'e', E);
    }
    return res;
}
/* J 型热电偶 */
InverseResult inverse_calculate_j(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -8.095 && E < 0.00)
    {
        InverseCoifficientsConfig j_config = {
            .d = {0},
            .error_min = -0.05,
            .error_max = 0.03,
            .order = 8};
        j_config.d[0] = 0.0000000E+00;
        j_config.d[1] = 1.9528268E+01;
        j_config.d[2] = -1.2286185E+00;
        j_config.d[3] = -1.0752178E+00;
        j_config.d[4] = -5.9086933E-01;
        j_config.d[5] = -1.7256713E-01;
        j_config.d[6] = -2.8131513E-02;
        j_config.d[7] = -2.3963370E-03;
        j_config.d[8] = -8.3823321E-05;
    }
    if (E >= 0.00 && E < 42.919)
    {
        InverseCoifficientsConfig j_config = {
            .d = {0},
            .error_min = -0.04,
            .error_max = 0.04,
            .order = 8};
        j_config.d[0] = 0.000000E+00;
        j_config.d[1] = 1.978425E+01;
        j_config.d[2] = -2.001204E-01;
        j_config.d[3] = 1.036969E-02;
        j_config.d[4] = -2.549687E-04;
        j_config.d[5] = 3.585153E-06;
        j_config.d[6] = -5.344285E-08;
        j_config.d[7] = 5.099890E-10;
        j_config.d[8] = 0.000000E+00;
        res = calculateT(&j_config, 'j', E);
    }
    if (E >= 42.919 && E <= 69.553)
    {
        InverseCoifficientsConfig j_config = {
            .d = {0},
            .error_min = -0.04,
            .error_max = 0.03,
            .order = 8};
        j_config.d[0] = -3.11358187E+03;
        j_config.d[1] = 3.00543684E+02;
        j_config.d[2] = -9.94773230E+00;
        j_config.d[3] = 1.70276630E-01;
        j_config.d[4] = -1.43033468E-03;
        j_config.d[5] = 4.73886084E-06;
        j_config.d[6] = 0.00000000E+00;
        j_config.d[7] = 0.00000000E+00;
        j_config.d[8] = 0.00000000E+00;
        res = calculateT(&j_config, 'j', E);
    }
    return res;
}
/* N 型热电偶 */
InverseResult inverse_calculate_n(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -3.990 && E < 0.00)
    {
        InverseCoifficientsConfig n_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.03,
            .order = 9};
        n_config.d[0] = 0.0000000E+00;
        n_config.d[1] = 3.8436847E+01;
        n_config.d[2] = 1.1010485E+00;
        n_config.d[3] = 5.2229312E+00;
        n_config.d[4] = 7.2060525E+00;
        n_config.d[5] = 5.8488586E+00;
        n_config.d[6] = 2.7754916E+00;
        n_config.d[7] = 7.7075166E-01;
        n_config.d[8] = 1.1582665E-01;
        n_config.d[9] = 7.3138868E-03;
        res = calculateT(&n_config, 'n', E);
    }
    if (E >= 0.00 && E <= 20.613)
    {
        InverseCoifficientsConfig n_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.03,
            .order = 9};
        n_config.d[0] = 0.00000E+00;
        n_config.d[1] = 3.86896E+01;
        n_config.d[2] = -1.08267E+00;
        n_config.d[3] = 4.70205E-02;
        n_config.d[4] = -2.12169E-06;
        n_config.d[5] = -1.17272E-04;
        n_config.d[6] = 5.39280E-06;
        n_config.d[7] = -7.98156E-08;
        n_config.d[8] = 0.00000E+00;
        n_config.d[9] = 0.00000E+00;
        res = calculateT(&n_config, 'n', E);
    }
    return res;
}
/* T型热电偶 */
InverseResult inverse_calculate_t(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -5.603 && E < 0.00)
    {
        InverseCoifficientsConfig t_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.04,
            .order = 7};
        t_config.d[0] = 0.0000000E+00;
        t_config.d[1] = 2.5949192E+01;
        t_config.d[2] = -2.1316967E-01;
        t_config.d[3] = 7.9018692E-01;
        t_config.d[4] = 4.2527777E-01;
        t_config.d[5] = 1.3304473E-01;
        t_config.d[6] = 2.0241446E-02;
        t_config.d[7] = 1.2668171E-03;
        res = calculateT(&t_config, 't', E);
    }
    if (E > 0 && E <= 20.872)
    {
        InverseCoifficientsConfig t_config = {
            .d = {0},
            .error_min = -0.03,
            .error_max = 0.03,
            .order = 7};
        t_config.d[0] = 0.000000E+00;
        t_config.d[1] = 2.592800E+01;
        t_config.d[2] = -7.602961E-01;
        t_config.d[3] = 4.637791E-02;
        t_config.d[4] = -2.165394E-03;
        t_config.d[5] = 6.048144E-05;
        t_config.d[6] = -7.293422E-07;
        t_config.d[7] = 0.000000E+00;
        res = calculateT(&t_config, 't', E);
    }
    return res;
}

/* R 型热电偶 */
InverseResult inverse_calculate_r(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -0.226 && E < 1.923)
    {
        InverseCoifficientsConfig r_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.02,
            .order = 10};
        r_config.d[0] = 0.0000000E+00;
        r_config.d[1] = 1.8891380E+02;
        r_config.d[2] = -9.3835290E+01;
        r_config.d[3] = 1.3068619E+02;
        r_config.d[4] = -2.2703580E+02;
        r_config.d[5] = 3.5145659E+02;
        r_config.d[6] = -3.8953900E+02;
        r_config.d[7] = 2.8239471E+02;
        r_config.d[8] = -1.2607281E+02;
        r_config.d[9] = 3.1353611E+01;
        r_config.d[10] = -3.3187769E+00;
        res = calculateT(&r_config, 'r', E);
    }
    if (E >= 1.923 && E < 13.228)
    {
        InverseCoifficientsConfig r_config = {
            .d = {0},
            .error_min = -0.005,
            .error_max = 0.005,
            .order = 10};
        r_config.d[0] = 1.334584505E+01;
        r_config.d[0] = 1.472644573E+02;
        r_config.d[0] = -1.844024844E+01;
        r_config.d[0] = 4.031129726E+00;
        r_config.d[0] = -6.249428360E-01;
        r_config.d[0] = 6.468412046E-02;
        r_config.d[0] = -4.458750426E-03;
        r_config.d[0] = 1.994710149E-04;
        r_config.d[0] = -5.313401790E-06;
        r_config.d[0] = 6.481976217E-08;
        r_config.d[0] = 0.000000000E+00;
        res = calculateT(&r_config, 'r', E);
    }
    if (E >= 11.361 && E < 19.739)
    {
        InverseCoifficientsConfig r_config = {
            .d = {0},
            .error_min = -0.0005,
            .error_max = 0.001,
            .order = 10};
        r_config.d[0] = -8.199599416E+01;
        r_config.d[1] = 1.553962042E+02;
        r_config.d[2] = -8.342197663E+00;
        r_config.d[3] = 4.279433549E-01;
        r_config.d[4] = -1.191577910E-02;
        r_config.d[5] = 1.492290091E-04;
        r_config.d[6] = 0.000000000E+00;
        r_config.d[7] = 0.000000000E+00;
        r_config.d[8] = 0.000000000E+00;
        r_config.d[9] = 0.000000000E+00;
        r_config.d[10] = 0.000000000E+00;
        res = calculateT(&r_config, 'r', E);
    }
    if (E >= 19.739 && E <= 21.103)
    {
        InverseCoifficientsConfig r_config = {
            .d = {0},
            .error_min = -0.001,
            .error_max = 0.002,
            .order = 10};
        r_config.d[0] = 3.406177836E+04;
        r_config.d[1] = -7.023729171E+03;
        r_config.d[2] = 5.582903813E+02;
        r_config.d[3] = -1.952394635E+01;
        r_config.d[4] = 2.560740231E-01;
        r_config.d[5] = 0.000000000E+00;
        r_config.d[6] = 0.000000000E+00;
        r_config.d[7] = 0.000000000E+00;
        r_config.d[8] = 0.000000000E+00;
        r_config.d[9] = 0.000000000E+00;
        r_config.d[10] = 0.000000000E+00;
        res = calculateT(&r_config, 'r', E);
    }
    return res;
}
/* S 型热电偶 */
InverseResult inverse_calculate_s(float E)
{
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    if (E >= -0.235 && E < 1.874)
    {
        InverseCoifficientsConfig s_config = {
            .d = {0},
            .error_min = -0.02,
            .error_max = 0.02,
            .order = 9};
        s_config.d[0] = 0.00000000E+00;
        s_config.d[1] = 1.84949460E+02;
        s_config.d[2] = -8.00504062E+01;
        s_config.d[3] = 1.02237430E+02;
        s_config.d[4] = -1.52248592E+02;
        s_config.d[5] = 1.88821343E+02;
        s_config.d[6] = -1.59085941E+02;
        s_config.d[7] = 8.23027880E+01;
        s_config.d[8] = -2.34181944E+01;
        s_config.d[9] = 2.79786260E+00;
        res = calculateT(&s_config, 's', E);
    }
    if (E >= 1.874 && E < 11.950)
    {
        InverseCoifficientsConfig s_config = {
            .d = {0},
            .error_min = -0.01,
            .error_max = 0.01,
            .order = 9};
        s_config.d[0] = 1.291507177E+01;
        s_config.d[1] = 1.466298863E+02;
        s_config.d[2] = -1.534713402E+01;
        s_config.d[3] = 3.145945973E+00;
        s_config.d[4] = -4.163257839E-01;
        s_config.d[5] = 3.187963771E-02;
        s_config.d[6] = -1.291637500E-03;
        s_config.d[7] = 2.183475087E-05;
        s_config.d[8] = -1.447379511E-07;
        s_config.d[9] = 8.211272125E-09;
        res = calculateT(&s_config, 's', E);
    }
    if (E >= 10.332 && E < 17.536)
    {
        InverseCoifficientsConfig s_config = {
            .d = {0},
            .error_min = -0.0002,
            .error_max = 0.0002,
            .order = 9};
        s_config.d[0] = -8.087801117E+0;
        s_config.d[1] = 1.621573104E+02;
        s_config.d[2] = -8.536869453E+00;
        s_config.d[3] = 4.719686976E-01;
        s_config.d[4] = -1.441693666E-02;
        s_config.d[5] = 2.081618890E-04;
        s_config.d[6] = 0.000000000E+00;
        s_config.d[7] = 0.000000000E+00;
        s_config.d[8] = 0.000000000E+00;
        s_config.d[9] = 0.000000000E+00;
        res = calculateT(&s_config, 's', E);
    }
    if (E >= 17.536 && E <= 18.693)
    {
        InverseCoifficientsConfig s_config = {
            .d = {0},
            .error_min = -0.002,
            .error_max = 0.002,
            .order = 9};
        s_config.d[0] = 5.333875126E+04;
        s_config.d[1] = -1.235892298E+04;
        s_config.d[2] = 1.092657613E+03;
        s_config.d[3] = -4.265693686E+01;
        s_config.d[4] = 6.247205420E-01;
        s_config.d[5] = 0.000000000E+00;
        s_config.d[6] = 0.000000000E+00;
        s_config.d[7] = 0.000000000E+00;
        s_config.d[8] = 0.000000000E+00;
        s_config.d[9] = 0.000000000E+00;
        res = calculateT(&s_config, 's', E);
    }
    return res;
}

int main()
{
    char type;
    float V = 0.0;
    int running = 1; // 用于控制循环的标志
    InverseResult res = {.t = 0.0, .error_range = {0.0, 0.0}};
    while (running)
    {
        printf("请输入热电偶类型(`q`退出): k/b/e/j/n/t/r/s\n");
        type = getchar();
        if (type >= 'A' && type <= 'Z')
            type += 32; // 转换为小写字母
        if (type == 'q')
        {
            running = 0; // 设置标志为0，退出循环
            break;
        }
        while (getchar() != '\n')
            ; // 清空输入缓冲区中的换行符
        printf("请输入测量电压值(mV): \n");
        int flag = scanf("%f", &V);
        if (flag != 1)
        {
            printf("输入错误，请重新输入\n");
            while (getchar() != '\n')
                ;     // 清空输入缓冲区中的错误字符
            continue; // 跳过当前循环，重新输入
        }
        // 清除输入缓冲区中可能残留的换行符（通常scanf读取成功后会留下一个换行符）
        while (getchar() != '\n')
            ;
        switch (type)
        {
        case 'k':
            res = inverse_calculate_k(V);
            break;
        case 'b':
            res = inverse_calculate_b(V);
            break;
        case 'e':
            res = inverse_calculate_e(V);
            break;
        case 'j':
            res = inverse_calculate_j(V);
            break;
        case 'n':
            res = inverse_calculate_n(V);
            break;
        case 't':
            res = inverse_calculate_t(V);
            break;
        case 'r':
            res = inverse_calculate_r(V);
            break;
        case 's':
            res = inverse_calculate_s(V);
            break;
        default:
            break;
        }
        printf("温度值为(摄氏度): %f, 误差范围: (%f, %f)\n\n", res.t, res.error_range[0], res.error_range[1]);
    }
    return 0;
}